package com.xinfu.newproject.ui.carFee;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.WebPayActivity;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.park_bean.bean.TempPayBean;
import com.xinfu.newproject.bean.paycost.bean.ComputeParkingBean;
import com.xinfu.newproject.presenter.ParkingPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.DateFormatUtils;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class StopPaymentActivity extends BaseActivity implements IView.ITempPayView {

    @BindView(R.id.activity_stop_payment_park_field)
    TextView fieldTv;

    @BindView(R.id.activity_stop_payment_park_car)
    TextView carIdTv;

    @BindView(R.id.activity_stop_payment_start_time)
    TextView startTimeTv;

    @BindView(R.id.activity_stop_payment_end_time)
    TextView endTimeTv;

    @BindView(R.id.activity_stop_payment_park_time)
    TextView parkTimeTv;

    @BindView(R.id.activity_stop_payment_total_cost)
    TextView totalTv;

    @BindView(R.id.activity_stop_payment_need_cost)
    TextView needCostTv;

    @BindView(R.id.activity_stop_payment_need_pay)
    TextView needPayTv;

    private ParkingPresenter parkingPresenter = new ParkingPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_payment);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("临停缴费");
        initViewBind();
        initLisener();
    }

    private void initLisener() {

    }

    private String userId;
    private String mobile;
    private String carType;
    private String carId;
    private String PKName;
    private Integer payAmount;
    private String PKID;
    private String tempOrderNo;
    private String tenantId;

    private void initViewBind() {
        String response = getIntent().getExtras().getString("response");
        ComputeParkingBean computeParkingBean = new Gson().fromJson(response, ComputeParkingBean.class);
        fieldTv.setText(computeParkingBean.getResult().getPkname());
        carIdTv.setText("车牌号码: " + computeParkingBean.getResult().getPlateNumber());
        startTimeTv.setText(computeParkingBean.getResult().getEntranceTime());
        endTimeTv.setText(DateFormatUtils.getYearMonthDayHourMinute(new Date()));
        parkTimeTv.setText(DateFormatUtils.getDistanceDays2(computeParkingBean.getResult().getEntranceTime()));
        totalTv.setText("￥: " + computeParkingBean.getResult().getPkorder().getAmount());
        needCostTv.setText("￥: " + computeParkingBean.getResult().getPkorder().getAmount());
        needPayTv.setText("￥: " + computeParkingBean.getResult().getPkorder().getAmount());

        PKID = computeParkingBean.getResult().getPKID();
        carType = "0";
        carId = computeParkingBean.getResult().getPlateNumber();
        PKName = computeParkingBean.getResult().getPkname();
        payAmount = (int) (computeParkingBean.getResult().getPkorder().getAmount() * 100);
        tempOrderNo = computeParkingBean.getResult().getPkorder().getOrderNo();
        tenantId = computeParkingBean.getResult().getTenantId();
    }

    @OnClick({R.id.activity_stop_payment_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_stop_payment_submit:
                if (isLogin()) {
                    parkingPresenter.tempPay();
                }
                break;
        }
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userName;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
            mobile = resultBean.getUserInfo().getPhone();

        } else {
            BToast.showText(this, "请先登录");
            return false;
        }

        return true;
    }


    @Override
    public String header() {
        return tenantId;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String mobile() {
        return mobile;
    }

    @Override
    public String carType() {
        return carType;
    }

    @Override
    public String carId() {
        return carId;
    }

    @Override
    public String PKName() {
        return PKName;
    }

    @Override
    public Integer payAmount() {
        return payAmount;
    }

    @Override
    public String PKID() {
        return PKID;
    }

    @Override
    public String tempOrderNo() {
        return tempOrderNo;
    }

    @Override
    public void showToast(String content) {
        BToast.showText(this, content);
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {
        showLoding();
    }

    @Override
    public void hideProgress() {
        hideLoding();
    }

    @Override
    public void getMsg(TempPayBean tempPayBean) {
        Bundle bundle = new Bundle();
        bundle.putString("response", tempPayBean.getResult().getPay_url());
        startIntentActivity(WebPayActivity.class, bundle);
        finish();
    }
}
